package client

import (
	"context"
	"google.golang.org/grpc"
	user "online-medical-consultation/common/proto"
)

type Handler func(ctx context.Context, in user.UserClient) (interface{}, error)

func UserClient(ctx context.Context, handler Handler) (interface{}, error) {
	dial, err := grpc.Dial("127.0.0.1:8082", grpc.WithInsecure())
	if err != nil {
		return nil, err
	}
	client := user.NewUserClient(dial)
	res, err := handler(ctx, client)
	if err != nil {
		return nil, err
	}
	return res, nil
}
func SendSms(ctx context.Context, i *user.SendSmsReq) (*user.SendSmsResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, in user.UserClient) (interface{}, error) {
		sms, err := in.SendSms(ctx, i)
		if err != nil {
			return nil, err
		}
		return sms, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.SendSmsResp), nil
}

func UserLogin(ctx context.Context, i *user.UserLoginReq) (*user.UserLoginResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, in user.UserClient) (interface{}, error) {
		login, err := in.UserLogin(ctx, i)
		if err != nil {
			return nil, err
		}
		return login, err
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.UserLoginResp), nil
}
func AddPatient(ctx context.Context, i *user.AddPatientReq) (*user.AddPatientResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, in user.UserClient) (interface{}, error) {
		patient, err := in.AddPatient(ctx, i)
		if err != nil {
			return nil, err
		}
		return patient, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.AddPatientResp), nil
}
func HospitalList(ctx context.Context, i *user.HospitalListReq) (*user.HospitalListResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, in user.UserClient) (interface{}, error) {
		list, err := in.HospitalList(ctx, i)
		if err != nil {
			return nil, err
		}
		return list, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.HospitalListResp), err
}
func HospitalToEs(ctx context.Context, i *user.HospitalToEsReq) (*user.HospitalToEsResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, in user.UserClient) (interface{}, error) {
		es, err := in.HospitalToEs(ctx, i)
		if err != nil {
			return nil, err
		}
		return es, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.HospitalToEsResp), nil
}
func SearchHospitalEs(ctx context.Context, i *user.SearchHospitalEsReq) (*user.HospitalListResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, in user.UserClient) (interface{}, error) {
		es, err := in.SearchHospitalEs(ctx, i)
		if err != nil {
			return nil, err
		}
		return es, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.HospitalListResp), nil
}
func DepartmentList(ctx context.Context, i *user.DepartmentListReq) (*user.DepartmentListResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, in user.UserClient) (interface{}, error) {
		list, err := in.DepartmentList(ctx, i)
		if err != nil {
			return nil, err
		}
		return list, nil
	})
	if err != nil {
		return &user.DepartmentListResp{}, err
	}
	return client.(*user.DepartmentListResp), nil
}
func SearchDepartment(ctx context.Context, i *user.SearchDepartmentReq) (*user.SearchDepartmentResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, in user.UserClient) (interface{}, error) {
		department, err := in.SearchDepartment(ctx, i)
		if err != nil {
			return nil, err
		}
		return department, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.SearchDepartmentResp), nil
}
func PatientList(ctx context.Context, i *user.PatientListReq) (*user.PatientListResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, in user.UserClient) (interface{}, error) {
		list, err := in.PatientList(ctx, i)
		if err != nil {
			return nil, err
		}
		return list, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.PatientListResp), nil
}
func DockerList(ctx context.Context, i *user.DockerListReq) (*user.DockerListResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, in user.UserClient) (interface{}, error) {
		list, err := in.DockerList(ctx, i)
		if err != nil {
			return nil, err
		}
		return list, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.DockerListResp), nil

}
func CreateOrder(ctx context.Context, i *user.CreateOrderReq) (*user.CreateOrderResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, in user.UserClient) (interface{}, error) {
		order, err := in.CreateOrder(ctx, i)
		if err != nil {
			return nil, err
		}
		return order, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.CreateOrderResp), nil
}
func OrderList(ctx context.Context, i *user.OrderListReq) (*user.OrderListResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, in user.UserClient) (interface{}, error) {
		list, err := in.OrderList(ctx, i)
		if err != nil {
			return nil, err
		}
		return list, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.OrderListResp), nil
}
func PayTime(ctx context.Context, i *user.PayTimeReq) (*user.PayTimeResp, error) {
	client, err := UserClient(ctx, func(ctx context.Context, in user.UserClient) (interface{}, error) {
		time, err := in.PayTime(ctx, i)
		if err != nil {
			return nil, err
		}
		return time, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.PayTimeResp), nil
}
